﻿using System;

namespace DotNetCommon.Validate.Annotations;

/// <summary>
/// 字符串必须匹配所有给定的正则表达式, 示例:
/// <code>
/// [VMustMatchRegexAll(["\\D1[0-9]{10}\\D", "电话"], errorMessage: "描述中必须有联系电话,如: 【联系电话: 13502412321】")]
/// public string Remark { get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustMatchRegexAllAttribute(string[] patterns, string errorMessage = null) : Attribute, IValidateAction
{
    public string[] Patterns { get; set; } = patterns;
    public string ErrorMessage { get; set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        ValidateStringExtensions.MustMatchRegexAllInternal(ctx, ctx.ModelValue?.ToString(), Patterns, ErrorMessage);
    }
}

/// <summary>
/// 如果不为null, 则字符串必须匹配所有给定的正则表达式, 示例:
/// <code>
/// [VMustMatchRegexAllIfNotNull(["\\D1[0-9]{10}\\D", "电话"], errorMessage: "描述中必须有联系电话,如: 【联系电话: 13502412321】")]
/// public string Remark { get; set; }
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class VMustMatchRegexAllIfNotNullAttribute(string[] patterns, string errorMessage = null) : Attribute, IValidateAction
{
    public string[] Patterns { get; set; } = patterns;
    public string ErrorMessage { get; set; } = errorMessage;

    public void Action(IValidateContext ctx)
    {
        if (ctx.ModelValue == null) return;
        ValidateStringExtensions.MustMatchRegexAllInternal(ctx, ctx.ModelValue?.ToString(), Patterns, ErrorMessage);
    }
}
